/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.CommentsHome;
import com.iti.jets.gpts.home.FeedbackHome;
import com.iti.jets.gpts.home.TranServiceHome;
import com.iti.jets.gpts.home.UserHome;
import com.iti.jets.gpts.pojo.Feedback;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author U
 */
@Path("/feedback")
public class FeedbacksService {

    TranServiceHome daotranservice = new TranServiceHome(util.HibernateUtil.getSessionFactory());
    UserHome daouser = new UserHome(util.HibernateUtil.getSessionFactory());
    FeedbackHome daofeedback = new FeedbackHome(util.HibernateUtil.getSessionFactory());
    CommentsHome daocomment = new CommentsHome(util.HibernateUtil.getSessionFactory());

    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/rate/{id}")
    public String calcFeedback(@PathParam(value = "id") int id) {

        return Integer.toString(daofeedback.calculateAverageFeedBack(id));
    }

    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/postfeed/{id}/{rate}/{email}")
    public String PostFeedback(@PathParam(value = "id") int id, @PathParam(value = "rate") int rate, @PathParam(value = "email") String email) {

        Feedback feedback = new Feedback();
        int length = daofeedback.getAllFeedbacks();
        feedback.setRating(rate);
        feedback.setTranService(daotranservice.getObjectByID(id));
        feedback.setUser(daouser.getObject(email));
        if (!daouser.isUserPostFeedbackBefore(daouser.getObject(email).getId())) {
            feedback.setId(length + 1);
            daofeedback.create(feedback);
        } else {
            Feedback feedback1 = daofeedback.getObject(daouser.getObject(email).getId());
            feedback1.setRating(rate);
            daofeedback.update(feedback1);
        }
        return "Done";
    }

    @GET
    @Path("/count/{id}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public String getNumberOfFeedbacks(@PathParam(value = "id") int id) {

        return daofeedback.getNumberOfFeedbacks(id);
    }

    //    @GET
//    @Produces(MediaType.APPLICATION_JSON)
//    @Path("/feed/{name}")
//    public JSONArray getFeedbacks(@PathParam(value = "name") String name) throws JSONException {
//        JSONArray jsonFeedbackArray = new JSONArray();
//        TranService tranService = new TranService();
//        tranService = daotranservice.getObject(name);
//        daofeedback.calculateAverageFeedBack(tranService.getId());
//        List result = new ArrayList(tranService.getFeedbacks());
//        for (Iterator it = result.iterator(); it.hasNext();) {
//            Feedback feedback = (Feedback) it.next();
//            JSONObject jSONObject = new JSONObject();
//            jSONObject.put("id", feedback.getId());
//            jSONObject.put("rate", feedback.getRating());
//            jSONObject.put("transervice", feedback.getTranService());
//            jSONObject.put("unit", feedback.getUnitIdentifier());
//            jSONObject.put("user", feedback.getUser());
//            jsonFeedbackArray.put(jSONObject);
//        }
//        return jsonFeedbackArray;
//    }
}
